<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../css/elementUI.css">
    <script src="../js/vue.js"></script>
    <script src="../js/axios.js"></script>
    <script src="../js/elementUI.js"></script>

</head>
<body>

<div id="app">

    <el-input v-model="query.name" placeholder="请输入商品名称" clearable style="width: 200px;"></el-input>
    <el-input v-model="query.money1" placeholder="请输入价格1" clearable style="width: 200px;"></el-input>
    <el-input v-model="query.money2" placeholder="请输入价格2" clearable style="width: 200px;"></el-input>
    <el-button type="primary" @click="toAdd">添加</el-button>
    <el-button type="danger" @click="delAll">批量删除</el-button>
    <el-table
            ref="multipleTable"
            :data="tableData"
            tooltip-effect="dark"
            style="width: 100%"
            @selection-change="handleSelectionChange">
        <el-table-column
                type="selection"
                width="55">
        </el-table-column>

        <el-table-column
                prop="id"
                label="订单编号">
        </el-table-column>

        <el-table-column
                prop="pname"
                label="商品名称">
        </el-table-column>


        <el-table-column
                prop="price"
                label="订单价格">
        </el-table-column>

        <el-table-column
                prop="createTime"
                label="创建时间"
                show-overflow-tooltip>
        </el-table-column>
        <el-table-column
                prop="state"
                label="订单状态"
                show-overflow-tooltip>
            <template slot-scope="scope">
                <span>{{scope.row.state==1?"已发货":"未发货"}}</span>
            </template>
        </el-table-column>

        <el-table-column label="操作">
            <template slot-scope="scope">
                <el-button
                        size="mini"
                        @click="handleEdit(scope.row)">编辑
                </el-button>
                <el-button
                        size="mini"
                        type="danger"
                        @click="handleDelete(scope.row)">删除
                </el-button>
            </template>
        </el-table-column>

    </el-table>

    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="query.page"
            :page-sizes="[2, 4, 6,10]"
            :page-size="query.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>


    <el-dialog :title="mode.title+'模板'" :visible.sync="dialogFormVisible" top="10px" :before-close="beforeCloseDialog">
        <el-form :model="form">
            <el-form-item label="商品名称" :label-width="formLabelWidth">
                <el-select v-model="form.pid" placeholder="请选择商品">
                    <el-option
                            v-for="p in productList"
                            :key="p.id"
                            :label="p.name"
                            :value="p.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="订单价格" :label-width="formLabelWidth">
                <el-input type="number" v-model="form.price" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="创建时间" :label-width="formLabelWidth">
                <el-input v-model="form.createTime" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="订单状态" :label-width="formLabelWidth">
                <template>
                    <el-radio v-model="form.state" :label="1">已发货</el-radio>
                    <el-radio v-model="form.state" :label="2">未发货</el-radio>
                </template>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="addBrand">确 定</el-button>
        </div>
    </el-dialog>

</div>

</body>
<script>
    new Vue({
        el: "#app",
        data: {
            productList:[],
            mode: {
                title: "",
                url: ""
            },
            fileList: [],
            formLabelWidth: "120px",
            query: {
                name: "",
                money1: "",
                money2: "",
                page: 1,
                size: 2
            },
            form: {
                pid: "",
                price: "",
                createTime: "",
                state: "",
            },
            tableData: [],
            total: 0,
            multipleSelection: [],
            dialogImageUrl: '',
            dialogVisible: false,
            dialogFormVisible: false
        },
        watch: {
            query: {
                deep: true,
                handler() {
                    this.initData()
                }
            }
        },
        methods: {
            handleEdit(row) {
                this.form = JSON.parse(JSON.stringify(row));//普通输入框的回显
                this.mode = {
                    title: "修改",
                    url: "/order/update.do"
                }

                // this.fileList = [{name: row.name, url: row.image}];
                this.dialogFormVisible = true;
            },
            delAll() {
                if (this.multipleSelection.length > 0) {
                    this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.delBrand(this.multipleSelection.map(m => m.id).join(","));
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });
                } else {
                    this.$message('请选择一条数据');
                }
            },
            handleDelete(row) {
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.delBrand(row.id);
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            delBrand(ids) {
                axios.get(`/order/deleteAll.do?ids=${ids}`).then(res => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.initData()
                })

            },
            beforeCloseDialog(close) {

                this.initFormData();
                close()//关闭弹窗的方法
            },
            addBrand() {
                axios.post(this.mode.url, this.form).then(res => {
                    this.initFormData();
                    this.initData()
                })
            },
            toAdd() {
                this.mode = {
                    title: "添加",
                    url: "/order/add.do"
                }
                this.form= {
                        pid: "",
                        price: "",
                        createTime: "",
                        state:2,
                }
                this.dialogFormVisible = true;
            },
            initFormData() {
                this.form = {
                    image: "",
                    name: "",
                    seq: "",
                    letter: ""
                }
                this.fileList = [];
                this.dialogFormVisible = false;

            },
            initData() {
                axios.post("/order/findPage.do",this.query).then(res => {
                    this.tableData = res.data.rows;
                    this.total = res.data.total;
                    if (this.tableData.length == 0 && this.query.page > 1) {
                        this.query.page--
                    }
                })

            },
            initProductData() {
                axios.post("/order/findProduct.do").then(res => {
                    this.productList=res.data;
                })

            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            handleSizeChange(val) {
                this.query.size = val
            },
            handleCurrentChange(val) {
                this.query.page = val
            },
            handleRemove(file, fileList) {
                this.form.image = "";
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            uploadSuccess(res) {
                this.form.image = res;
            }
        },
        created() {
            this.initData();
            this.initProductData();
        }
    })
</script>
</html>